﻿using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.Tools.WindowsInstallerXml.Bootstrapper;

namespace Wix.Win8SetupBootstrapper.ViewModels
{
    public class ViewModelBase: INotifyPropertyChanged
    {
        private readonly BootstrapperApplication _bootstrapper;

        public BootstrapperApplication Bootstrapper
        {
            get { return _bootstrapper; }
        }
        
        public ViewModelBase(BootstrapperApplication bootstrapper)
        {
            _bootstrapper = bootstrapper;
        }


        public string ReadVariableFromBundle(string variableName)
        {
            string result = string.Empty;
            try
            {
                string text = string.Format("Getting variable '{0}' from the bundle.", variableName);
                this._bootstrapper.Engine.Log(LogLevel.Verbose, text);
                return this._bootstrapper.Engine.StringVariables[variableName];
            }
            catch (System.Exception ex)
            {
                string text2 = string.Format("The variable '{0}' does not exist in the bundle.\n{1}", variableName,
                    ex.Message);
                this._bootstrapper.Engine.Log(LogLevel.Error, text2);
            }
            return result;
        }
        public void WriteVariableToBundle(string variableName, string variableValue)
        {
            try
            {
                string text = string.Format("Writing variable '{0}' with the value '{1}' to the bundle.", variableName, variableValue);
                this._bootstrapper.Engine.Log(LogLevel.Verbose, text);

                this._bootstrapper.Engine.StringVariables[variableName] = variableValue;
            }
            catch (System.Exception ex)
            {
                string text2 = string.Format("Error writing variable '{0}' with the value {1} to bundle.\n{2}", variableName, variableValue, ex.Message);
                this._bootstrapper.Engine.Log(LogLevel.Error, text2);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        } 
    }
}